package org.daochong.lang.security;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.security.KeyStore;

public class StoreData {
	private String filePath;
	private InputStream input;
	private String password;
	private String storeType = "JKS";
	private String provider;
	private OutputStream outputStream;

	public static StoreData getInstance(String storePath, String storePass) {
		return getInstance(storePath, storePass, "JKS", null);
	}

	public static StoreData getInstance(String storePath, String storePass, String storeType) {
		return getInstance(storePath, storePass, storeType, null);
	}

	public static StoreData getInstance(String storePath, String storePass, String storeType, String provider) {
		StoreData data = new StoreData();
		data.setFilePath(storePath);
		data.setPassword(storePass);
		data.setStoreType(storeType);
		data.setProvider(provider);
		return data;
	}

	public StoreData() {

	}

	public String getStoreType() {
		return storeType;
	}

	public void setStoreType(String storeType) {
		this.storeType = storeType;
	}

	public OutputStream getOutputStream() {
		if (this.outputStream == null) {
			try {
				return new FileOutputStream(this.getFilePath());
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return outputStream;
	}

	public void setOutputStream(OutputStream outputStream) {
		this.outputStream = outputStream;
	}

	public String getProvider() {
		return provider;
	}

	public void setProvider(String provider) {
		this.provider = provider;
	}

	public String getFilePath() {
		return filePath;
	}

	public void setFilePath(String filePath) {
		this.filePath = filePath;
	}

	public InputStream getInput() {
		if (this.input == null && this.filePath != null) {
			try {
				File file = new File(this.filePath);
				if (file.exists()) {
					return new FileInputStream(this.filePath);
				}
			} catch (Throwable e) {

			}
		}
		return input;
	}

	public void setInput(InputStream input) {
		this.input = input;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public KeyStore getKeyStore() {
		KeyStore keyStore = null;
		try {
			if (this.getProvider() != null) {
				keyStore = KeyStore.getInstance(this.getStoreType(), this.getProvider());
			} else {
				keyStore = KeyStore.getInstance(this.getStoreType());
			}
			InputStream in = getInput();
			keyStore.load(in, this.getPassword().toCharArray());
			if (in != null) {
				in.close();
			}
			return keyStore;
		} catch (RuntimeException e) {
			throw e;
		} catch (Throwable e) {
			throw new RuntimeException("Get KeyStore Fail", e);
		}
	}

	public void save(KeyStore keyStore) {
		try {
			keyStore.store(this.getOutputStream(), this.getPassword().toCharArray());
		} catch (Throwable e) {
			throw new RuntimeException("Save KeyStore Fail", e);
		}
	}
}
